package com.yylending.las.lending.process;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.content.Loader;
import android.view.KeyEvent;
import com.yyfax.common.CLog;
import com.yyfax.infrastructure.mvp.Presenter;
import com.yylending.las.R;
import com.yylending.las.base.BaseActivity;
import com.yylending.las.Constants;
import com.yylending.las.StatIDSet;
import com.yyfax.infrastructure.statistics.StatService;
import com.yylending.las.lending.process.chooseamount.ChooseAmountFragment;
import com.yylending.las.lending.process.confirmloan.ConfirmLoanFragment;
import com.yylending.las.lending.process.result.NoSiteInvestigationFragment;
import com.yylending.las.lending.process.result.SiteInvestigationFragment;
import com.yylending.las.lending.process.supplyinfo.SupplementInfoFragment;
import com.yylending.las.lending.quota.myquota.MyQuotaModel;
import com.yylending.las.personal.bandcard.BoundBankCards;

import java.util.ArrayList;
import java.util.List;

public class ProcessActivity extends BaseActivity {

	private static final String TAG = ProcessActivity.class.getSimpleName();
	public static final String ARG_APPLY_STATUS = "arg_apply_status";
	public static final String BANK_INFO = "bank_info";
	private static final String CHOOSE_AMOUNT_TAG = "100";
	private static final String SUPPLEMENT_INFO_TAG = "101";
	private static final String CONFIRM_LOAD_TAG = "102";
	private static final String PAGE_RESULT_TAG = "103";
	private static final int PACE_CHOOSE_AMOUNT = 100;
	private static final int PAGE_SUPPLEMENT_INFO = 101;
	private static final int PAGE_CONFIRM_LOAD = 102;
	private static final int PAGE_RESULT = 103;

	private int mCurrentPage;
	Fragment mChooseAmountFragment;
	Fragment mSupplementInfoFragment;
	public Fragment mConfirmLoadFragment;
	Fragment mResultFragment;
	public static String mStatus;
	public static Process mProcess = new Process();

	public static void start(Activity activity, MyQuotaModel.ApplyStatus status, List<BoundBankCards.BankInfo> bankInfo) {
		Intent intent = new Intent(activity, ProcessActivity.class);
		intent.putExtra(ARG_APPLY_STATUS,status);
		intent.putExtra(BANK_INFO, (ArrayList<? extends Parcelable>) bankInfo);
		activity.startActivity(intent);
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_process);
		showFragment(PACE_CHOOSE_AMOUNT);
	}

	@Override
	public void onLoadFinished(Loader<Presenter> loader, Presenter data, int placeholder) {

	}

	@Override
	public Loader<Presenter> onCreateLoader(int id, Bundle args) {
		return null;
	}

	/**
	 * 上一页
	 */
	private void pageUp() {
		switch (mCurrentPage) {
			case PAGE_CONFIRM_LOAD:
				if (((ConfirmLoanFragment)mConfirmLoadFragment).dismiss()) {
					return;
				}
				showFragment(PAGE_SUPPLEMENT_INFO);
				break;
			case PAGE_SUPPLEMENT_INFO:
				if(((SupplementInfoFragment) mSupplementInfoFragment).dismiss()){
					return;
				}
				showFragment(PACE_CHOOSE_AMOUNT);
				break;
		}
	}

	/**
	 * 下一页
	 */
	public void pageDown() {
		switch (mCurrentPage) {
			case PACE_CHOOSE_AMOUNT:
				showFragment(PAGE_SUPPLEMENT_INFO);
				break;
			case PAGE_SUPPLEMENT_INFO:
				showFragment(PAGE_CONFIRM_LOAD);
				break;
			case PAGE_CONFIRM_LOAD:
				showFragment(PAGE_RESULT);
				break;
		}
	}

	/**
	 * 跳转到结果页
	 */
	public void showResult() {
		showFragment(PAGE_RESULT);
	}

	public void showFragment(int page) {
		CLog.i(TAG, "showFragment page=" + page);
		FragmentManager fm = getSupportFragmentManager();
		FragmentTransaction ft = fm.beginTransaction();
		mChooseAmountFragment = fm.findFragmentByTag(CHOOSE_AMOUNT_TAG);
		mSupplementInfoFragment = fm.findFragmentByTag(SUPPLEMENT_INFO_TAG);
		mConfirmLoadFragment = fm.findFragmentByTag(CONFIRM_LOAD_TAG);
		mResultFragment = fm.findFragmentByTag(PAGE_RESULT_TAG);
		if (mChooseAmountFragment != null) {
			ft.hide(mChooseAmountFragment);
		}
		if (mSupplementInfoFragment != null) {
			ft.hide(mSupplementInfoFragment);
		}
		if (mConfirmLoadFragment != null) {
			ft.hide(mConfirmLoadFragment);
		}
		if (mResultFragment != null) {
			ft.hide(mResultFragment);
		}
		switch (page) {
			case PACE_CHOOSE_AMOUNT:
				if (mChooseAmountFragment == null) {
					mChooseAmountFragment = ChooseAmountFragment.newInstance();
					ft.add(R.id.process_fl, mChooseAmountFragment, CHOOSE_AMOUNT_TAG);
				} else {
					ft.show(mChooseAmountFragment);
				}
				setTitle(R.string.choose_sum);
				mCurrentPage = page;
				break;
			case PAGE_SUPPLEMENT_INFO:
				if (mSupplementInfoFragment == null) {
					mSupplementInfoFragment = SupplementInfoFragment.newInstance();
					ft.add(R.id.process_fl, mSupplementInfoFragment, SUPPLEMENT_INFO_TAG);
				} else {
					ft.show(mSupplementInfoFragment);
				}
				setTitle(R.string.supplement_Info);
				mCurrentPage = page;
				break;
			case PAGE_CONFIRM_LOAD:
				if (mConfirmLoadFragment == null) {
					mConfirmLoadFragment = ConfirmLoanFragment.newInstance();
					ft.add(R.id.process_fl, mConfirmLoadFragment, CONFIRM_LOAD_TAG);
				} else {
					ft.show(mConfirmLoadFragment);
				}
				setTitle(R.string.confirm_loan);
				mCurrentPage = page;
				break;
			case PAGE_RESULT:
				if (mResultFragment == null) {
					if (mStatus.equals(Constants.LOAN_STATUS_NOT_DIRECT)) {
						mResultFragment = SiteInvestigationFragment.newInstance();
					} else {
						mResultFragment = NoSiteInvestigationFragment.newInstance();
					}
					ft.add(R.id.process_fl, mResultFragment, PAGE_RESULT_TAG);
				} else {
					ft.show(mResultFragment);
				}
				setTitle(R.string.submit_loan_success);
				mCurrentPage = page;
				break;
			default:
				break;
		}
		ft.commitAllowingStateLoss();
	}

	@Override
	protected void onBackBtnPressed() {
		if (mCurrentPage != PACE_CHOOSE_AMOUNT && mCurrentPage != PAGE_RESULT) {
			pageUp();
		} else {
			super.onBackBtnPressed();
		}
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if ((keyCode == KeyEvent.KEYCODE_BACK) && mCurrentPage != PACE_CHOOSE_AMOUNT && mCurrentPage != PAGE_RESULT) {
			pageUp();
			return false;
		} else {
			return super.onKeyDown(keyCode, event);
		}

	}

}
